package com.zzyl.nursing.controller;

import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.nursing.domain.Device;
import com.zzyl.nursing.dto.DeviceDto;
import com.zzyl.nursing.service.IDeviceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 设备管理Controller
 * 
 * @author ruoyi
 * @date 2025-08-01
 */
@RestController
@RequestMapping("/nursing/device")
@Api(tags = "设备管理相关接口")
public class DeviceController extends BaseController
{
    @Autowired
    private IDeviceService deviceService;


    /**
     * 修改设备
     */
    @PutMapping
    public AjaxResult updateDevice(@RequestBody DeviceDto dto){
        boolean result = deviceService.updateDevice(dto);
        return result ? success() : error();
    }


    /**
     * 查询设备上报的数据
     */
    @GetMapping("/queryServiceProperties/{iotId}")
    public AjaxResult queryServiceProperties(@PathVariable("iotId") String iotId) {
        List<Map<String, Object>> list = deviceService.queryServiceProperties(iotId);
        return success(list);
    }

    /**
     * 查询设备详情数据
     */
    @GetMapping("/{iotId}")
    public AjaxResult getInfo(@PathVariable String iotId)
    {
        return success(deviceService.queryDeviceDetail(iotId));
    }


    /**
     * 查询设备管理列表
     */
    @PreAuthorize("@ss.hasPermi('nursing:device:list')")
    @GetMapping("/list")
    @ApiOperation(value = "获取设备管理列表")
    public TableDataInfo<List<Device>> list(Device device)
    {
        startPage();
        List<Device> list = deviceService.selectDeviceList(device);
        return getDataTable(list);
    }

    @PostMapping("/syncProductList")
    public AjaxResult syncProductList()
    {
        deviceService.syncProductList();
        return success();
    }

    /**
     * 查询所有产品列表
     */
    @GetMapping("/allProduct")
    public AjaxResult selectAllProductList() {
        return success(deviceService.selectAllProductList());
    }

    /**
     * 注册设备
     */
    @PostMapping("/register")
    public AjaxResult registerDevice(@RequestBody DeviceDto deviceDto){
        deviceService.registerDevice(deviceDto);
        return success();
    }

    /**
     * 删除设备
     */
    @DeleteMapping("/{iotId}")
    public AjaxResult deleteDevice(@PathVariable String iotId) {
        return deviceService.deleteDevice(iotId) ? success() : error();
    }

    /**
     * 查询设备详细数据
     */
    @GetMapping("/queryProduct/{productKey}")
    public AjaxResult queryDeviceDetail(@PathVariable String productKey) {
        return success(deviceService.queryProduct(productKey));
    }


}
